package org.emet.web.ro;

import org.emet.jee.console.dao.sys.DBDao;
import org.emet.jee.console.entity.sys.DB;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RemotingDestination
public class DbRo {
	@Autowired
	private DBDao dao;

	@Transactional(readOnly = true)
	public DB getDB() {
		return dao.findUniqueBy("id", 1L);
	}

	@Transactional
	public void update(String dbHome, String bakPath) {
		DB entity = dao.findUniqueBy("id", 1L);
		if (entity == null) {
			SQLQuery q = dao.getSession().createSQLQuery("insert into sys_db(id,db_home,back_up_path) values (?,?,?)");
			q.setParameter(0, 1L);
			q.setParameter(1, dbHome);
			q.setParameter(2, bakPath);
			q.executeUpdate();
		} else {
			entity.setBackUpPath(bakPath);
			entity.setDbHome(dbHome);
		}
	}

}
